/*
 * PConfiguracionBalance.java
 *
 * Created on 12 de diciembre de 2006, 11:02 AM
 */

package ig.metrica.ui.confBalance;

import ig.metrica.ejb.conbalance.ConfiguracionbalancesDTO;
import ig.util.MyTableModel;
import ig.util.NoContinuaException;
import java.util.Collection;
import java.util.Iterator;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author  jsanchez
 */
public class PConfiguracionBalance extends javax.swing.JPanel {
    MConfiguracionBalance modelo=null;
    /** Creates new form PConfiguracionBalance */
    public PConfiguracionBalance() {
        initComponents();
        modelo=new MConfiguracionBalance(this);
        this.TConfiguracionBalance.setModel(new MyTableModel(null,new String [] {"Cuenta PUC", "%", "Dias"},new boolean [] {true, true, true}));
        this.TConfiguracionBalance.updateUI();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        TConfiguracionBalance = new javax.swing.JTable();
        BNueva = new javax.swing.JButton();
        BEliminar = new javax.swing.JButton();

        TConfiguracionBalance.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Cuenta PUC", "%", "Dias"
            }
        ));
        jScrollPane1.setViewportView(TConfiguracionBalance);

        BNueva.setFont(new java.awt.Font("Dialog", 0, 12));
        BNueva.setText(org.openide.util.NbBundle.getMessage(PConfiguracionBalance.class, "PConfiguracionBalance.BNueva.text")); // NOI18N
        BNueva.setToolTipText(org.openide.util.NbBundle.getMessage(PConfiguracionBalance.class, "PConfiguracionBalance.BNueva.toolTipText")); // NOI18N
        BNueva.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 10, 1, 10));
        BNueva.setSelected(true);
        BNueva.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BNuevaActionPerformed(evt);
            }
        });

        BEliminar.setFont(new java.awt.Font("Dialog", 0, 12));
        BEliminar.setText(org.openide.util.NbBundle.getMessage(PConfiguracionBalance.class, "PConfiguracionBalance.BEliminar.text")); // NOI18N
        BEliminar.setToolTipText(org.openide.util.NbBundle.getMessage(PConfiguracionBalance.class, "PConfiguracionBalance.BEliminar.toolTipText")); // NOI18N
        BEliminar.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 10, 1, 10));
        BEliminar.setSelected(true);
        BEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BEliminarActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 306, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                    .add(BNueva, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(BEliminar))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(BNueva, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 25, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(BEliminar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 25, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    
    private void BEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BEliminarActionPerformed
        try {
            this.eliminar();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }//GEN-LAST:event_BEliminarActionPerformed
    
    private void BNuevaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BNuevaActionPerformed
        try {
            this.nuevo();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }//GEN-LAST:event_BNuevaActionPerformed
    
    public void setEnabledAll(boolean b) {
        this.BEliminar.setEnabled(b);
        this.BNueva.setEnabled(b);
        ((javax.swing.JViewport)this.jScrollPane1.getComponents()[0]).getComponents()[0].setEnabled(b);
    }
    
    public void eliminar(){
        if(this.TConfiguracionBalance.getSelectedRow()>-1 && this.TConfiguracionBalance.getSelectedRow()<TConfiguracionBalance.getRowCount()){
            if(JOptionPane.showConfirmDialog(null,"Elimiar la fila seleccionada","Mensaje",JOptionPane.OK_CANCEL_OPTION)==0){
                Object[][] Data= ((MyTableModel)this.TConfiguracionBalance.getModel()).getData();
                Object[][] DataAux=new Object[Data.length-1][3];
                int row=TConfiguracionBalance.getSelectedRow();
                boolean borrado=false;
                int k=0;
                for (int i = 0; i < Data.length; i++) {
                    for (int j = 0; j < 3; j++) {
                        if(row!=i){
                            DataAux[k][j]=Data[i][j];
                            borrado=false;
                        }else
                            borrado=true;
                    }
                    if(!borrado)
                        k++;
                }
                ((MyTableModel)this.TConfiguracionBalance.getModel()).setData(DataAux);
                this.TConfiguracionBalance.repaint();
                this.TConfiguracionBalance.updateUI();  
            }
        }else
            JOptionPane.showMessageDialog(null,"No se ha seleccionado ninguna fila","Mensaje",JOptionPane.OK_OPTION);
    }
    
    public void nuevo() throws NoContinuaException {
        Object[][] Data=((MyTableModel)this.TConfiguracionBalance.getModel()).getData();
        Object[][] DataAux=new Object[Data.length+1][3];
        for (int i = 0; i < Data.length; i++) {
            for (int j = 0; j < 3; j++) {
                DataAux[i][j]=Data[i][j];
            }
        }
        DataAux[Data.length][0]=0;
        DataAux[Data.length][1]=0;
        DataAux[Data.length][2]=0;
        this.TConfiguracionBalance.setModel(new MyTableModel(DataAux,new String [] {"Cuenta PUC", "%", "Dias"},new boolean [] {true, true, true}));
        //((MyTableModel)this.TConfiguracionBalance.getModel()).setData(DataAux);
        //this.TConfiguracionBalance.repaint();
        this.TConfiguracionBalance.updateUI();
    }
    
//    public void ComboItemStateChanged(java.awt.event.ItemEvent evt){
//        System.out.println("FILA:"+this.TConfiguracionBalance.getSelectedRow());
//        if(this.TConfiguracionBalance.getSelectedRow()+1==this.TConfiguracionBalance.getRowCount())
//            System.out.println("El otro evento del combo");
//    }
    
//    this.jTable1.getColumnModel().getColumn(1).setCellEditor( new DefaultCellEditor(this.jComboBox1));
    JComboBox combo= new JComboBox();
    public void setNombreModelo(String string)throws Exception {
        modelo.setNombreModelo(string);
        modelo.buscar();
        Collection CConfBalance=modelo.getConfiBalance();        
        System.out.println("CConfBalance:"+CConfBalance.size());
        Object[][] data=new Object[CConfBalance.size()][3];
        int i=0;
        for (Iterator it = CConfBalance.iterator(); it.hasNext();) {
            ConfiguracionbalancesDTO elem = (ConfiguracionbalancesDTO) it.next();
            data[i][0]=elem.getPuc();
            data[i][1]=elem.getProcentaje();
            data[i][2]=elem.getDuracion();
            i++;
        }
        this.TConfiguracionBalance.setModel(new MyTableModel(data,new String [] {"Cuenta PUC", "%", "Dias"},new boolean [] {true, true, true}));
        this.TConfiguracionBalance.updateUI();
        
        modelo.cargarContratosSimples();
        Collection PUC=modelo.getPUC();
        if(PUC!=null && PUC.size()>0)
            combo.setModel(new javax.swing.DefaultComboBoxModel(PUC.toArray()));        
        if(PUC!=null && PUC.size()>0)
            this.TConfiguracionBalance.getColumnModel().getColumn(0).setCellEditor( new DefaultCellEditor(this.combo));        
        this.TConfiguracionBalance.updateUI();
        
        TConfiguracionBalance.getTableHeader().setReorderingAllowed(false);
    }
    
    public void guardar()throws NoContinuaException {
        modelo.guardar();
    }
    
    public Object[][] getData(){
        return ((MyTableModel)this.TConfiguracionBalance.getModel()).getData();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BEliminar;
    private javax.swing.JButton BNueva;
    public javax.swing.JTable TConfiguracionBalance;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

}
